using System;
using System.Linq;
using System.Web.Mvc;
using Quickie.Models;
using Quickie.Models.IoC;
using Quickie.Models.Data;
using Quickie.Models.Entities;
using Quickie.Models.ViewModel;
using Quickie.Models.Interfaces;

namespace Quickie.Controllers
{
    [Authorize(Roles = "Administrator,Korisnik")]
    public class ProductController : Controller
    {
        private IRepository<Product> products;
        private IRepository<Category> categories;

        public ProductController(IRepository<Product> pr, IRepository<Category> cat)
        {
            products = pr;
            categories = cat;
        }

        public ActionResult Index()
        {
            var model = new ProductsHomeViewModel(); 

            model.BestPriced = products.GetAll()
                .OrderBy(x => x.Price)
                .Take(5)
                .ToList();

            return View(model);
        }

        public ActionResult ByCategory(int id)
        {
            var cat = categories.GetByID(id);
            var model = new ProductsViewModel();

            model.Title = cat.Name;
            model.CategoryID = cat.ID;
            model.Products = products.FromCategory(id);

            return View(model);
        }

        public ActionResult Details(int id)
        {
            Product model = products.GetByID(id);
            return View(model);
        }

        public FileContentResult GetImage(int productID)
        {
            Product product = products.GetByID(productID);
            
            return File(product.ImageData.ToArray(), product.ImageMimeType);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SearchByName(string searchByName)
        {
            var model = new Product();
            
            try
            {
                model = products.GetAll().Where(x => x.Name == searchByName).First();
            }
            catch (System.InvalidOperationException ex)
            {
                return View("Error");
            }
            

            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SearchByID(string searchByID)
        {
            var model = new Product();

            try
            {
                model = products.GetAll().Where(x => x.ID == Convert.ToInt32(searchByID)).First();
            }
            catch (System.InvalidOperationException ex)
            {
                return View("Error");
            }

            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SearchByCategory(int searchByCategory)
        {
            var cat = categories.GetByID(searchByCategory);
            var model = new ProductsViewModel();

            model.Title = cat.Name;
            model.CategoryID = cat.ID;
            model.Products = products.FromCategory(searchByCategory);

            return View("ByCategory", model);
        }
        
    }
}
